package com.demo01.d02;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: root
 * @Date: 2018/11/15 22:54
 */
public class CountDownLatchTest {
    public static void main(String[] args) {
        System.out.println("main is start.....");
        CountDownLatch latch = new CountDownLatch(3);
        Worker workerA = new Worker(latch,"workerA");
        Worker workerB = new Worker(latch,"workerB");
        Worker workerC = new Worker(latch,"workerC");
        new Thread(workerA).start();
        new Thread(workerB).start();
        new Thread(workerC).start();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main is finish.....");
    }
}

class Worker implements Runnable {
    private CountDownLatch countDownLatch;
    private String name;

    public Worker() {

    }

    public Worker(CountDownLatch countDownLatch, String name) {
        this.countDownLatch = countDownLatch;
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("worker : "+name+" is begin......");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("worker : "+name+" is end......");
        countDownLatch.countDown();
    }
}